package com.graduate.after.utils;

import com.graduate.after.constants.StatusCode;

import java.io.Serializable;

public class Result implements Serializable {

    /**
     * 响应结果状态码
     */
    private Integer code;

    /**
     * 响应结果的信息
     */
    private String message;

    /**
     * 响应结果的时间
     */
    private Long time;

    /**
     * 响应结果的数据
     */
    private Object data;

    public Result() {}

    public Result(StatusCode statusCode) {
        this(statusCode,null);
    }

    public Result(StatusCode statusCode, Object data) {
        this.code = statusCode.getCode();
        this.message = statusCode.getMessage();
        this.time = System.currentTimeMillis();
        this.data = data;
    }
    /**
     * 添加数据等操作成功时无需返回数据
     */
    public static Result success(){
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 成功时只需返回数据,状态码写死
     */
    public static Result success(Object obj){
        return new Result(StatusCode.SUCCESS,obj);
    }

    /**
     * 操作失败时无数据返回，但是需要传回失败原因(即状态码和信息)
     */
    public static Result fail(StatusCode statusCode){
        return new Result(statusCode);
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Long getTime() {
        return time;
    }

    public Object getData() {
        return data;
    }
}
